<?php
namespace app\devel\model;

use think\Model;
class TermModel extends Model
{
    static private $default = [
        'name'         => 'default',
        'type'         => 0,
        'term_tpl'     => 'list',
        'term_tags'    => [],
        'content_tpl'  => 'content',
        'content_tags' => [],
        'create_time'  => 0,
    ];

    public function forAdd($modelArr)
    {
        $modelList = [];
        if(!is_array($modelArr)) return false;
        foreach($modelArr as $key => $info){
            $add = $this->initModel($info);
            $modelId = $this->add($add);
            if($modelId === false)
                return array('Add_Error',$info);
            $modelList[$key] = $modelId;
        }
        return $modelList;
    }
    
    static private function initModel($info)
    {
        $time = time();
        $info = array_intersect_key($info, self::$default);
        $info = array_merge(self::$default, $info);
        if(empty($info['create_time'])) $info['create_time']=$time;
        if(is_array($info['term_tags']))
            $info['term_tags'] = json_encode($info['term_tags']);
        if(is_array($info['content_tags']))
            $info['content_tags'] = json_encode($info['content_tags']);
        return $info;
    }
}

